package com.miyu.system.dao.user;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.plugins.baseDao.BaseDao;
import com.miyu.system.pojo.system.SysUser;
import com.miyu.system.utils.page.PageLimit;
import jakarta.persistence.criteria.Predicate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


/**
 * 系统用户数据层
 * @author miyu
 */

@Repository
public interface SysUserDao extends BaseDao<SysUser,String> {
    /**
     * 查询系统用户
     * @param params 参数个数
     * @return {@link JSONArray}
     */
    default  Page<SysUser> querySysUser(JSONObject params){
        String userName;
        if(params.containsKey("userName")){
            userName = params.getString("userName");
        } else {
            userName = "";
        }
        Specification<SysUser> specification =  (root, criteriaQuery, criteriaBuilder) -> {
            // 查询条件的集合
            List<Predicate> list = new ArrayList<>();
            if(StrUtil.isNotBlank(userName))
                list.add(criteriaBuilder.like(root.get("userName"), userName));
            // 转数组
            Predicate[] predicates = new Predicate[list.size()];
            list.toArray(predicates);
            return criteriaBuilder.and(predicates);
        };

        return findAll(specification,PageLimit.pageable(params));

    }
}

